package com.haifeng.questionbankcrud.dao.impl;

import com.haifeng.questionbankcrud.dao.SingleChoiceDao;
import com.haifeng.questionbankcrud.pojo.SingleChoice;
import com.haifeng.questionbankcrud.util.DBUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class SingleChoiceDaoImpl implements SingleChoiceDao {
    private Connection conn = null;
    private Statement st = null;
    private ResultSet sr = null;

    public SingleChoiceDaoImpl() {
        try {
            /**
             * 适用druid作为连接池
             */
            conn = DBUtils.getConnection();
            st = conn.createStatement();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<SingleChoice> selectChoiceByPoint(SingleChoice sc) {
        //todo 查询的知识点使用枚举

        List<SingleChoice> singleChoiceList = new ArrayList<>();
        String sql = "select * from singleChoice where point=?";
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setString(1, sc.getPoint());
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                SingleChoice item = new SingleChoice();
                item.setQuestion(resultSet.getString("question"));
                item.setOption_a(resultSet.getString("option_a"));
                item.setOption_b(resultSet.getString("option_b"));
                item.setOption_c(resultSet.getString("option_c"));
                item.setOption_d(resultSet.getString("option_d"));
                item.setAnswer(resultSet.getString("answer"));
                item.setPoint(resultSet.getString("point"));
                singleChoiceList.add(item);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return singleChoiceList;
        }


        return singleChoiceList;
    }

    @Override
    public List<SingleChoice> selectAllChoice() {

        List<SingleChoice> singleChoiceList = new ArrayList<>();
        String sql = "select * from singleChoice;";
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = conn.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                SingleChoice item = new SingleChoice();
                item.setQuestion(resultSet.getString("question"));
                item.setOption_a(resultSet.getString("option_a"));
                item.setOption_b(resultSet.getString("option_b"));
                item.setOption_c(resultSet.getString("option_c"));
                item.setOption_d(resultSet.getString("option_d"));
                item.setAnswer(resultSet.getString("answer"));
                item.setPoint(resultSet.getString("point"));
                singleChoiceList.add(item);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return singleChoiceList;
        }


        return singleChoiceList;
    }

    @Override
    public int insertSingleChoice(SingleChoice sc) {
        int msg = 0;
        String sql = "insert into singleChoice values (null,?,?,?,?,?,?,?)";
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setString( 1, sc.getQuestion());
            preparedStatement.setString( 2, sc.getOption_a());
            preparedStatement.setString( 3, sc.getOption_b());
            preparedStatement.setString( 4, sc.getOption_c());
            preparedStatement.setString( 5, sc.getOption_d());
            preparedStatement.setString( 6, sc.getAnswer());
            preparedStatement.setString( 7, sc.getPoint());
            msg = preparedStatement.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
            return msg;
        }

        return msg;
    }

    @Override
    public int deleteSingleChoiceById(SingleChoice sc) {
        int msg = 0;

        String sql = "delete from singleChoice  where q_id=?";
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setInt(1, sc.getQ_id());
            msg = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            return msg;
        }
        return msg;
    }

    @Override
    public int updateSingleChoiceById(SingleChoice sc) {

        int msg = 0;

        String sql = "update singleChoice set question=?,option_a=?,option_b=?,option_c=?,option_d=?,answer=?,point=?where q_id=?";
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setString(1, sc.getQuestion());
            preparedStatement.setString(2, sc.getOption_a());
            preparedStatement.setString( 3, sc.getOption_b());
            preparedStatement.setString( 4, sc.getOption_c());
            preparedStatement.setString( 5, sc.getOption_d());
            preparedStatement.setString( 6, sc.getAnswer());
            preparedStatement.setString( 7, sc.getPoint());
            preparedStatement.setInt( 8,sc.getQ_id());
            msg = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            return msg;
        }
        return msg;
    }
}
